package com.be.beadmin.project.controller;

import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.project.dto.output.PayFileInfoEx;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.data.domain.Page;
import com.be.beadmin.project.service.PayFileInfoService;
import com.be.beadmin.project.dto.input.PayFileInfoQueryCriteria;
import com.be.beadmin.project.dto.input.PayFileInfoDto;
import org.springframework.data.domain.Pageable;
import com.be.beadmin.common.utils.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.io.IOException;
import java.util.List;
import com.alibaba.excel.EasyExcel;
import org.springframework.web.multipart.MultipartFile;

/**
* @author zjm
* @date 2023-08-31
**/
@Api(tags = "付款信息附件")
@RestController
public class PayFileInfoController {
    private String bucketName = "payinfo";
    @Autowired
    private  PayFileInfoService payFileInfoService;
    @ApiOperation("付款信息附件上传")
    @PostMapping("/design/uploadFileByPayInfo")
    public ResultModel uploadFileByPayInfo(HttpServletRequest request,
                                           @ApiParam(value = "项目id",required = true) @RequestParam String infoId,
                                           @ApiParam(value = "款项名称",required = true) @RequestParam(required = true) String stageName,
                                           @ApiParam(value = "款项附件上传",required = true) @RequestParam(value="file") MultipartFile[] multipartFile
    ) throws Exception {
        payFileInfoService.uploadFileByPayInfo(request, infoId,stageName,multipartFile,bucketName);
        return ResultModel.success("附件上传成功");
    }

    @ApiOperation("获取付款信息附件")
    @PostMapping("/design/getFileByPayInfo")
    public ResultModel getFileByPayInfo(HttpServletRequest request,
                                           @ApiParam(value = "项目id",required = true) @RequestParam String infoId,
                                           @ApiParam(value = "款项名称",required = true) @RequestParam(required = true) String stageName
    ) throws Exception {

        return ResultModel.success(payFileInfoService.getFileByPayInfo(request, infoId,stageName,bucketName));
    }

    @PostMapping(value = "/project/getPayFileInfoByPage")
    public ResultModel getPayFileInfoByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(payFileInfoService.getPayFileInfoByPage(pageEntity)) ;
    }

    @PostMapping("/project/addPayFileInfo")
    public ResultModel addPayFileInfo(@RequestBody PayFileInfoDto payFileInfoDto){
        payFileInfoService.addPayFileInfo(payFileInfoDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/project/delPayFileInfo")
    public ResultModel delPayFileInfo(@RequestBody PayFileInfoDto payFileInfoDto){
        payFileInfoService.delPayFileInfo(payFileInfoDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/project/updatePayFileInfo")
    public ResultModel updatePayFileInfo(@RequestBody PayFileInfoDto payFileInfoDto){
        payFileInfoService.updatePayFileInfo(payFileInfoDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @ApiOperation("获得付款信息pdf、图片的预览url")
    @PostMapping("/quality/getPayPreviewUrl")
    public ResultModel getPayPreviewUrl(@RequestParam String payFileInfoId) throws Exception {
        String previewUrl =  payFileInfoService.getPayPreviewUrl(payFileInfoId) ;
        return ResultModel.success(previewUrl);
    }

    @PostMapping("/project/exportPayFileInfo")
    public void exportPayFileInfo(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<PayFileInfoEx> payFileInfoPage = payFileInfoService.getPayFileInfoByPage(pageEntity);
        List<PayFileInfoEx> payFileInfoExes = payFileInfoPage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("PayFileInfo接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), PayFileInfoEx.class).sheet("sheet1").doWrite(payFileInfoExes);
    }
}